package com.ruoyi.project.tool.swagger.AppApi;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysCode;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.domain.SysDictType;
import com.ruoyi.project.system.domain.SysPatrol;
import com.ruoyi.project.system.mapper.SysDictDataMapper;
import com.ruoyi.project.system.mapper.SysDictTypeMapper;
import com.ruoyi.project.system.service.ISysCodeService;
import com.ruoyi.project.system.service.ISysDictDataService;
import com.ruoyi.project.system.service.ISysDictTypeService;
import com.ruoyi.project.system.service.ISysPatrolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * 巡查Controller
 * */

@RestController
@RequestMapping("/appapi/patrol")
public class PatrolController extends BaseController {
    @Autowired
    private ISysPatrolService sysPatrolService;

    @Autowired
    private ISysDictTypeService sysDictTypeService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private ISysCodeService sysCodeService;

    /**
     * 查询巡查列表
     */
    @GetMapping("/list")
    public Object list(SysPatrol sysPatrol) {
        startPage();
        List<SysPatrol> list = sysPatrolService.selectSysPatrolList(sysPatrol);
        Map<Object, Object> map = new HashMap<Object, Object>();
        List arrayList=new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            String patrolType = list.get(i).getPatrolType();
            Long codeId = list.get(i).getCodeId();
            SysCode sysCode = sysCodeService.selectSysCodeById(codeId);
            String address = sysCode.getAddress();
            SysDictData dictData = sysDictDataService.selectDictUrl(patrolType);
            if (dictData.getUrl() == null && dictData.getUrl().equals("")) {
                return AjaxResult.error();
            }
            String url = dictData.getUrl();
            String s = "patrolType:" + patrolType + "  url:" + url+"  address:"+address;
            arrayList.add(s);
        }
        map.put("patrolType", arrayList);
        map.put("list", list);
        return map;
    }

    /**
     * 获取巡查详细信息
     */
    @GetMapping(value = "/{patrolId}")
    public AjaxResult getInfo(@PathVariable("patrolId") Long patrolId) {
        return AjaxResult.success(sysPatrolService.selectSysPatrolById(patrolId));
    }

    /**
     * 新增巡查
     */
    @Log(title = "巡查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysPatrol sysPatrol) {
        return toAjax(sysPatrolService.insertSysPatrol(sysPatrol));
    }

    /**
     * 修改巡查
     */
    @Log(title = "巡查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysPatrol sysPatrol) {
        return toAjax(sysPatrolService.updateSysPatrol(sysPatrol));
    }

    /**
     * 删除巡查
     */
    @Log(title = "巡查", businessType = BusinessType.DELETE)
    @DeleteMapping("/{patrolIds}")
    public AjaxResult remove(@PathVariable Long[] patrolIds) {
        return toAjax(sysPatrolService.deleteSysPatrolByIds(patrolIds));
    }
}
